home *** CD-ROM | disk | FTP | other *** search
/ Windows Expert / Windows Expert.iso / program / vbpxen.zip / PXMODULE.BAS < prev    next >
BASIC Source File  |  1991-10-07  |  14KB  |  417 lines

  1. '
  2. '  Written by Steve Jackson 
  3. '             9152 Brabham Drive
  4. '             Huntington Beach, CA 92646
  5. '
  6. '  Thanks to John Jaster for some of the dll definitions
  7. '
  8. '  Most of the engine functions are defined here, but not all.
  9. '  One that I have not gotten to work is PxErrMsg because it returns
  10. '  a pointer.  Visual Basic has no pointer types (that I know of).  
  11. '  You might get it to work by get a pointer to windows memory and
  12. '  using that, but it is beyond me right now.
  13. '
  14. '  This module is meant to be a general purpose visual basic interface
  15. '  to the Paradox engine DLL.  To run it, you need the DLL from Paradox
  16. '  Engine.  An example of usage is distributed in little video rental 
  17. '  application called VVDEMO.  
  18. '
  19. '  Comments, questions are welcome.  If you know of any ways I can
  20. '  earn a little extra income to purchase a faster computer (and with
  21. '  more memory) that would be welcome too.
  22. '******* Declarations for Using the Paradox 3.5 Engine ******
  23. Declare Function PXWinInit Lib "Pxengwin.dll" (ByVal Application$, ByVal Mode%) As Integer
  24. Declare Function PXExit Lib "Pxengwin.dll" () As Integer
  25. '************ TABLE FUNCTIONS *****************
  26. Declare Function PXTblOpen Lib "Pxengwin.dll" (ByVal TblName$, TblHnd%, ByVal index%, ByVal change%) As Integer
  27. Declare Function PXTblClose Lib "Pxengwin.dll" (ByVal TblHnd%) As Integer
  28. '************* RECORD FUNCTIONS *******************
  29. Declare Function PXRecAppend Lib "Pxengwin.dll" (ByVal TblHnd%, ByVal RecHnd%) As Integer
  30. Declare Function PXRecInsert Lib "Pxengwin.dll" (ByVal TblHnd%, ByVal RecHnd%) As Integer
  31. Declare Function PXRecUpdate Lib "Pxengwin.dll" (ByVal TblHnd%, ByVal RecHnd%) As Integer
  32. Declare Function PXRecDelete Lib "Pxengwin.dll" (ByVal TblHnd%) As Integer
  33. Declare Function PXRecBufOpen Lib "Pxengwin.dll" (ByVal TblHnd%, RecHnd%) As Integer
  34. Declare Function PXRecBufClose Lib "Pxengwin.dll" (ByVal RecHnd%) As Integer
  35. Declare Function PXRecBufEmpty Lib "Pxengwin.dll" (ByVal RecHnd%) As Integer
  36. Declare Function PXRecGet Lib "Pxengwin.dll" (ByVal TblHnd%, ByVal RecHnd%) As Integer
  37. Declare Function PXRecFirst Lib "Pxengwin.dll" (ByVal TblHnd%) As Integer
  38. Declare Function PXRecLast Lib "Pxengwin.dll" (ByVal TblHnd%) As Integer
  39. Declare Function PXRecNext Lib "Pxengwin.dll" (ByVal TblHnd%) As Integer
  40. Declare Function PXRecPrev Lib "Pxengwin.dll" (ByVal TblHnd%) As Integer
  41. Declare Function PXRecNum Lib "Pxengwin.dll" (ByVal TblHnd%, RecNum%) As Integer
  42. Declare Function PXTblNRecs Lib "Pxengwin.dll" (ByVal TblHnd%, nRecs%) As Integer
  43. '**************** FIELD FUNCTIONS ****************
  44. Declare Function PXPutShort Lib "Pxengwin.dll" (ByVal RecHnd%, ByVal FldHnd%, ByVal sValue%) As Integer
  45. Declare Function PXPutDoub Lib "Pxengwin.dll" (ByVal RecHnd%, ByVal FldHnd%, ByVal dValue#) As Integer
  46. Declare Function PXPutLong Lib "Pxengwin.dll" (ByVal RecHnd%, ByVal FldHnd%, ByVal lValue&) As Integer
  47. Declare Function PXPutAlpha Lib "Pxengwin.dll" (ByVal RecHnd%, ByVal FldHnd%, ByVal aValue$) As Integer
  48. Declare Function PXPutBlank Lib "Pxengwin.dll" (ByVal RecHnd%, ByVal FldHnd%) As Integer
  49. Declare Function PXPutDate Lib "Pxengwin.dll" (ByVal RecHnd%, ByVal FldHnd%, ByVal inDate As Any) As Integer
  50. Declare Function PXGetShort Lib "Pxengwin.dll" (ByVal RecHnd%, ByVal FldHnd%, sValue%) As Integer
  51. Declare Function PXGetDoub Lib "Pxengwin.dll" (ByVal RecHnd%, ByVal FldHnd%, dValue#) As Integer
  52. Declare Function PXGetLong Lib "Pxengwin.dll" (ByVal RecHnd%, ByVal FldHnd%, lValue&) As Integer
  53. Declare Function PXGetAlpha Lib "Pxengwin.dll" (ByVal RecHnd%, ByVal FldHnd%, ByVal bufSize%, ByVal aValue$) As Integer
  54. Declare Function PXFldBlank Lib "Pxengwin.dll" (ByVal RecHnd%, ByVal FldHnd%, Blank%) As Integer
  55. Declare Function PXGetDate Lib "Pxengwin.dll" (ByVal RecHnd%, ByVal FldHnd%, outDate As Any) As Integer
  56. Declare Function PXRecNFlds Lib "Pxengwin.dll" (ByVal TblHnd%, nFlds%) As Integer
  57. Declare Function PXFldHandle Lib "Pxengwin.dll" (ByVal TblHnd%, ByVal FldName$, FldHnd%) As Integer
  58. Declare Function PXFldType Lib "Pxengwin.dll" (ByVal TblHnd%, ByVal FldHnd%, ByVal BufSiz%, ByVal fldtype$) As Integer
  59. Declare Function PXFldName Lib "Pxengwin.dll" (ByVal TblHnd%, ByVal FldHnd%, ByVal BufSiz%, ByVal FldName$) As Integer
  60. '*************** SEARCH FUNCTIONS *******************
  61. Declare Function PXSrchKey Lib "Pxengwin.dll" (ByVal TblHnd%, ByVal RecHnd%, ByVal nFlds%, ByVal Mode%) As Integer
  62. Declare Function PXSrchFld Lib "Pxengwin.dll" (ByVal TblHnd%, ByVal RecHnd%, ByVal FldNum%, ByVal Mode%) As Integer
  63. '***************** MISCELLANEOUS FUNCTIONS ****************
  64. Declare Function PXDateDecode Lib "Pxengwin.dll" (ByVal outDate As Any, mm%, dd%, yy%) As Integer
  65. Declare Function PXDateEncode Lib "Pxengwin.dll" (ByVal mm%, ByVal dd%, ByVal yy%, pDate&) As Integer
  66. ' note: PXErrMsg returns a string, not an integer
  67. Declare Function PXErrMsg Lib "Pxengwin.dll" (ByVal error_code%) As String
  68. '******************* NETWORK FUNCTIONS ******************
  69. Declare Function PXNetUserName Lib "Pxengwin.dll" (ByVal buffer%, UserName$) As Integer
  70. Declare Function PXNetFileLock Lib "Pxengwin.dll" (ByVal FileName$, ByVal lockType%) As Integer
  71. Declare Function PXNetFileUnlock Lib "Pxengwin.dll" (ByVal FileName$, ByVal lockType%) As Integer
  72. Declare Function PXNetTblLock Lib "Pxengwin.dll" (ByVal TblHnd%, ByVal lockType%) As Integer
  73. Declare Function PXNetTblUnlock Lib "Pxengwin.dll" (ByVal TblHnd%, ByVal lockType%) As Integer
  74. Declare Function PXNetRecLock Lib "Pxengwin.dll" (ByVal TblHnd%, LockHnd%) As Integer
  75. Declare Function PXNetRecUnlock Lib "Pxengwin.dll" (ByVal TblHnd%, ByVal LockHnd%) As Integer
  76. Declare Function PXNetRecLocked Lib "Pxengwin.dll" (ByVal TblHnd%, Locked%) As Integer
  77. Declare Function PXNetTblChanged Lib "Pxengwin.dll" (ByVal TblHnd%, Changed%) As Integer
  78. Declare Function PXNetTblRefresh Lib "Pxengwin.dll" (ByVal TblHnd%) As Integer
  79. '
  80. ' Variables used only in this module
  81. '
  82. ' What must be defined in global:  NUMBER_OF_TABLES
  83. '
  84. '
  85. Dim hTable(NUMBER_OF_TABLES) As Integer
  86. Dim hRecBuf(NUMBER_OF_TABLES) As Integer
  87. Dim hRecLock(NUMBER_OF_TABLES) As Integer
  88. Dim iTableIsClosed(NUMBER_OF_TABLES) As Integer
  89.  
  90. Dim alpha_field As String * 256
  91. Dim px As Integer
  92.  
  93. Const PX_OK = 0
  94. Const PX_ENDOFTABLE = 101
  95. Const PX_STARTOFTABLE = 102
  96. Const PX_RECNOTFOUND = 89
  97. Const PX_KEYVIOL = 97
  98. Const PX_RECDELETED = 50
  99. Const PX_RECLOCKED = 9
  100.  
  101. Sub PXError (ByVal error_code As Integer)
  102.     '
  103.     '  General purpose error trapping.
  104.     '  If the error is not critical (that is, the database is OK),
  105.     '  return to the user.  Store message that they can retrieve if
  106.     '  needed by calling dberrormsg().
  107.     '
  108.     '  If the error is critical, processing cannot continue, and
  109.     '  this routine will END THE PROGRAM
  110.     '
  111.     If error_code = PX_OK Then
  112.         Exit Sub
  113.     End If
  114.     '
  115.     '   Non-critical errors:
  116.     '
  117.     Select Case error_code
  118.         Case PX_OK
  119.             Exit Sub
  120.         Case PX_ENDOFTABLE, PX_STARTOFTABLE, PX_KEYVIOL
  121.             Exit Sub
  122.         Case PX_RECNOTFOUND, PX_RECDELETED
  123.             Exit Sub
  124.     End Select
  125.  
  126.     Msg$ = "Paradox database error code: " + Str$(error_code)
  127.     ' alpha_field = PXErrMsg(error_code)
  128.     ' Msg$ = Msg$ + alpha_field
  129.     MsgBox Msg$, 0 + 16, "Database Error"
  130.     End
  131. End Sub
  132.  
  133. Function DBInit (ByVal AppName$) As Integer
  134.     '
  135.     ' Start the paradox engine for windows
  136.     ' for now always use mode of: PXSHARED
  137.     '
  138.     px = PXWinInit(AppName$, 2)
  139.     If px = 82 Then
  140.         DBInit = PX_OK
  141.         Exit Function
  142.     End If
  143.  
  144.     If px Then
  145.         Msg$ = "Unable to start Paradox engine, code: " + Str$(px)
  146.         Msg$ = Msg$ + " Remember to type SHARE before starting Windows"
  147.         MsgBox Msg$, 0 + 16, "Database Initialization"
  148.         End
  149.     End If
  150.  
  151.     DBInit = PX_OK
  152. End Function
  153.  
  154. Function DBExit () As Integer
  155.     '
  156.     '  Shutdown the paradox engine
  157.     '
  158.     DBExit = PXExit()
  159. End Function
  160.  
  161. Function TableOpen (ByVal Tblnum%, ByVal TblName$)
  162.     '
  163.     '  Open a table and allocate one record buffer for it.
  164.     '  Application calls this routine once for each table.
  165.     '  Note that it creates table and record handles for use in
  166.     '  other database routines.  They get the correct handles by
  167.     '  indexing into the handle array with the application assigned
  168.     '  table id - should be a const in their global declaration,
  169.     '  and MUST be sequentially assigned starting at ZERO.
  170.     '
  171.     px = PXTblOpen(TblName$, TblHnd%, 0, TRUE)
  172.     PXError (px)
  173.  
  174.     px = PXRecBufOpen(TblHnd%, RecHnd%)
  175.     PXError (px)
  176.  
  177.     px = PXRecBufEmpty(RecHnd%)
  178.     PXError (px)
  179.  
  180.     hTable(Tblnum%) = TblHnd%
  181.     hRecBuf(Tblnum%) = RecHnd%
  182.  
  183.     TableOpen = PX_OK
  184. End Function
  185.  
  186. Function GetRec (ByVal Tblnum%, ByVal Action%)
  187.     '
  188.     '  Get a record and move it to the record buffer.
  189.     '  Note that it uses table and record handles created in TableOpen()
  190.     '
  191.     hTbl% = hTable(Tblnum%)
  192.     hrec% = hRecBuf(Tblnum%)
  193.  
  194.     Select Case Action%
  195.          Case DBKEYED
  196.             px = PXSrchKey(hTbl%, hrec%, 1, 0)
  197.             PXError (px)
  198.          Case DBFIRST
  199.             px = PXRecFirst(hTbl%)
  200.             '  check for end, not found, etc.
  201.             PXError (px)
  202.          Case DBNEXT
  203.             px = PXRecNext(hTbl%)
  204.             PXError (px)
  205.          Case DBPRIOR
  206.             px = PXRecPrev(hTbl%)
  207.             PXError (px)
  208.          Case DBLAST
  209.             px = PXRecLast(hTbl%)
  210.             PXError (px)
  211.     End Select
  212.  
  213.     If px Then
  214.         GetRec = px
  215.         Exit Function
  216.     End If
  217.  
  218.     px = PXRecGet(hTbl%, hrec%)
  219.     PXError (px)
  220.  
  221.     GetRec = PX_OK
  222. End Function
  223.  
  224. '
  225. Function UpdateRec (ByVal Tblnum%) As Integer
  226.     '
  227.     '  Uupdate the record that is current  (last one retrieved)
  228.     '
  229.     hTbl% = hTable(Tblnum%)
  230.     hrec% = hRecBuf(Tblnum%)
  231.  
  232.     px = PXRecUpdate(hTbl%, hrec%)
  233.     PXError (px)
  234.  
  235.     UpdateRec = px
  236.  
  237. End Function
  238.  
  239. Function AddRec (ByVal Tblnum%) As Integer
  240.     '
  241.     '  Add a new record.  If file is not indexed, goes at end
  242.     '
  243.     hTbl% = hTable(Tblnum%)
  244.     hrec% = hRecBuf(Tblnum%)
  245.  
  246.     px = PXRecAppend(hTbl%, hrec%)
  247.     PXError (px)
  248.  
  249.     AddRec = px
  250.  
  251. End Function
  252.  
  253. Function DeleteRec (ByVal Tblnum%) As Integer
  254.     '
  255.     '  Delete current record (most recently retrieved)
  256.     '
  257.     hTbl% = hTable(Tblnum%)
  258.  
  259.     px = PXRecDelete(hTbl%)
  260.     PXError (px)
  261.  
  262.     DeleteRec = px
  263.  
  264. End Function
  265.  
  266. Function PutAlphaField (ByVal TableNum%, ByVal FieldNum%, ByVal FieldVal$) As Integer
  267.     '
  268.     '  Move field to paradox buffer
  269.     '
  270.     hrec% = hRecBuf(TableNum%)
  271.     alpha_field = FieldVal$
  272.  
  273.     px = PXPutAlpha(hrec%, FieldNum%, alpha_field)
  274.     PXError (px)
  275.  
  276.     PutAlphaField = PX_OK
  277.  
  278. End Function
  279.  
  280. Function PutShortField (ByVal TableNum%, ByVal FieldNum%, ByVal ShortVal%) As Integer
  281.     '
  282.     '  Move field to paradox buffer
  283.     '
  284.     hrec% = hRecBuf(TableNum%)
  285.     
  286.     px = PXPutShort(hrec%, FieldNum%, ShortVal%)
  287.     PXError (px)
  288.  
  289.     PutShortField = PX_OK
  290.  
  291. End Function
  292.  
  293. Function PutNumField (ByVal TableNum%, ByVal FieldNum%, ByVal NumVal) As Integer
  294.     Dim nDouble As Double
  295.     '
  296.     '  Move field to paradox buffer
  297.     '
  298.     hrec% = hRecBuf(TableNum%)
  299.     nDouble = NumVal
  300.  
  301.     px = PXPutDoub(hrec%, FieldNum%, nDouble)
  302.     PXError (px)
  303.  
  304.     PutNumField = PX_OK
  305.  
  306. End Function
  307.  
  308. Function GetAlphaField (ByVal TableNum%, ByVal FieldNum%, FieldVal$) As Integer
  309.     Dim IsBlank As Integer
  310.     '
  311.     '  Get field from paradox buffer to user buffer
  312.     '
  313.     hrec% = hRecBuf(TableNum)
  314.  
  315.     px = PXFldBlank(hrec%, FieldNum%, IsBlank)
  316.     PXError (px)
  317.      
  318.     If IsBlank Then
  319.         FieldVal$ = " "
  320.         GetAlphaField = PX_OK
  321.         Exit Function
  322.     End If
  323.         
  324.     px = PXGetAlpha(hrec%, FieldNum%, 255, alpha_field)
  325.     PXError (px)
  326.  
  327.     FieldVal$ = alpha_field
  328.     GetAlphaField = PX_OK
  329. End Function
  330.  
  331. Function GetShortField (ByVal TableNum%, ByVal FieldNum%, ShortVal%) As Integer
  332.     '
  333.     '  Get field from paradox buffer to user buffer
  334.     '
  335.     Dim iShort As Integer
  336.  
  337.     hrec% = hRecBuf(TableNum)
  338.  
  339.     px = PXGetShort(hrec%, FieldNum%, iShort)
  340.     PXError (px)
  341.  
  342.     ShortVal% = iShort
  343.     GetShortField = PX_OK
  344. End Function
  345.  
  346. Function GetNumField (ByVal TableNum%, ByVal FieldNum%, NumVal) As Integer
  347.     '
  348.     '  Get field from paradox buffer to user buffer
  349.     '
  350.     Dim nDouble As Double
  351.  
  352.     hrec% = hRecBuf(TableNum)
  353.  
  354.     px = PXGetDoub(hrec%, FieldNum%, nDouble)
  355.     PXError (px)
  356.  
  357.     NumVal = nDouble
  358.     GetNumField = PX_OK
  359. End Function
  360.  
  361. Function LockRec (ByVal Tblnum%) As Integer
  362.     Dim iLockHandle As Integer
  363.     '
  364.     '  Lock the record that is current  (last one retrieved)
  365.     '
  366.     hTbl% = hTable(Tblnum%)
  367.  
  368.     px = PXNetRecLock(hTbl%, iLockHandle)
  369.     If px = PX_RECLOCKED Then
  370.         LockRec = DB_RECLOCKED
  371.         Exit Function
  372.     End If
  373.     '
  374.     '  check for any other critical error
  375.     '
  376.     PXError (px)
  377.  
  378.     hRecLock(Tblnum%) = iLockHandle
  379.  
  380.     LockRec = px
  381. End Function
  382.  
  383. Function UnlockRec (ByVal Tblnum%) As Integer
  384.     Dim iLockHandle As Integer
  385.     '
  386.     '  Unock a record.
  387.     '  In this version, only one record per table can be
  388.     '  locked at any time.  Could change in the future
  389.     '
  390.     hTbl% = hTable(Tblnum%)
  391.     iLockHandle = hRecLock(Tblnum%)
  392.     '
  393.     '  If no record is locked, exit the function
  394.     '
  395.     If iLockHandle = 0 Then
  396.         UnlockRec = DB_OK
  397.         Exit Function
  398.     End If
  399.     
  400.     px = PXNetRecUnlock(hTbl%, iLockHandle)
  401.     '
  402.     '  If the unlock failed, just go ahead and return
  403.     '  This is REALLY sloppy coding, should be fixed soon
  404.     '
  405.     If px = 110 Then
  406.         UnlockRec = PX_SUCCESS
  407.         Exit Function
  408.     End If
  409.  
  410.     PXError (px)
  411.  
  412.     hRecLock(Tblnum%) = 0
  413.     UnlockRec = px
  414. End Function
  415.  
  416.